"use strict";


const DateUtil = require("../utils/time");
const BasicsCtl = require("../basics/clz/basectl");
const HomeImpl = require("../service/home");
const UserApi = require("../taobao/user");

class HomeCtl extends BasicsCtl {

  constructor(context) {
    super(context);
    this.homeImpl = new HomeImpl(this.cloud);
  }

  /**
   * 查询淘宝会员
   */
  async findMember() {
    let { mixNick, sourceMiniAppId } = this.params;
    let master = await this.homeImpl.querySessionId(sourceMiniAppId);
    return await UserApi.selectMember(this.cloud, master.session, mixNick);
  }

  /**
   * 查询基础配置
   */
  async findBaseActConf() {
    return await this.homeImpl.queryBaseActConf();
  }

  /**
   * 查询店铺
   */
  async findShopId() {
    let { sourceMiniAppId, openId, userNick } = this.params;
    console.log("sourceMiniAppId", sourceMiniAppId);
    let data = await this.homeImpl.queryShopId(sourceMiniAppId);
    if (data) {
      data.openId = openId;
      data.userNick = userNick;
      data.nowtime = new Date().getTime();
    }
    return data;
  }

  /**
   * 查询店铺
   */
  async findShopOne() {
    let { sellerId, openId, userNick } = this.params;
    console.log("sellerId", sellerId);
    let data = await this.homeImpl.queryShopOne(sellerId);
    if (data) {
      data.openId = openId;
      data.userNick = userNick;
      data.nowtime = new Date().getTime();
    }
    return data;
  }

  /**
   * 白名单
   */
  async findIsWhite() {
    // return true;
    let { openId } = this.params;
    console.log("userNick", openId);
    return await this.homeImpl.queryIsWhite(openId);
  }
  /**
   * 白名单
   */
  async addWhite() {
    let { openId } = this.params;
    console.log("userNick", openId);
    return await this.homeImpl.addWhite(openId);
  }
  /**
   * 查询配置
   */
  async findRebase() {
    return await this.homeImpl.findRebase();
  }
  /**
   * 查询配置
   */
  async findJump() {
    let { code } = this.params;
    if (!code) {
      return null;
    }
    return await this.homeImpl.findJump(code);
  }

  /**
   * 查询sessionKey
   */
  async querySessionId() {
    let { sourceMiniAppId } = this.params;
    return await this.homeImpl.querySessionId(sourceMiniAppId);
  }

}
module.exports = HomeCtl;